﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Data.Common;
using System.Data;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class NewsData
    {
        public static DataTable GetAllNews(int currentPageIndex, string sortOrder,
            int pageSize, string searchTitle, DateTime? searchCreateDateFrom, DateTime? searchCreateDateTo,
            bool? searchActive, bool? searchFeatured, out int totalData)
        {
            totalData = 0;
            object count = null;

            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@SortOrder", DbType.String, 500, ParameterDirection.Input, sortOrder);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            dataMiner.AddParameter(command, "@SearchTitle", DbType.String, 2000, ParameterDirection.Input, searchTitle);
            dataMiner.AddParameter(command, "@SearchCreateDateFrom", DbType.DateTime, 0, ParameterDirection.Input, searchCreateDateFrom);
            dataMiner.AddParameter(command, "@SearchCreateDateTo", DbType.DateTime, 0, ParameterDirection.Input, searchCreateDateTo);
            dataMiner.AddParameter(command, "@SearchActive", DbType.Boolean, 0, ParameterDirection.Input, searchActive);
            dataMiner.AddParameter(command, "@SearchFeatured", DbType.Boolean, 0, ParameterDirection.Input, searchFeatured);

            command.CommandText = "CSP_GetTotalNewsCount";
            count = dataMiner.GetOneData(command);
            totalData = (int)CommonUtility.FilterNull(count, typeof(int));

            command.CommandText = "CSP_GetNewsList";
            return dataMiner.GetListData(command);
        }

        public static int CreateNewNews(string title, string shortDescription,
            string description, bool isActive, bool isFeatured)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreateNewNews";
            dataMiner.AddParameter(command, "@Title", DbType.String, 2000, ParameterDirection.Input, title);
            dataMiner.AddParameter(command, "@ShortDescription", DbType.String, -1, ParameterDirection.Input, shortDescription);
            dataMiner.AddParameter(command, "@Description", DbType.String, -1, ParameterDirection.Input, description);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@IsFeatured", DbType.Boolean, 0, ParameterDirection.Input, isFeatured);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static int DeleteNews(List<int> announcementsToDelete)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeleteNews";
            dataMiner.AddParameter(command, "@Ids", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(announcementsToDelete));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int UpdateFeaturedNews(List<int> featuredNews,
            List<int> nonFeaturedNews)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateFeaturedNews";
            dataMiner.AddParameter(command, "@NewsIdsToFeatured", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(featuredNews));
            dataMiner.AddParameter(command, "@NewsIdsToNonFeatured", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(nonFeaturedNews));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static int UpdateActiveNews(List<int> activeNews,
            List<int> inactiveNews)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateActiveNews";
            dataMiner.AddParameter(command, "@NewsIdsToActive", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(activeNews));
            dataMiner.AddParameter(command, "@NewsIdsToInactive", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(inactiveNews));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static DataTable GetLatestNews(int howMany)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            command.CommandText = "CSP_GetLatestNewsList";
            dataMiner.AddParameter(command, "@HowMany", DbType.Int32, 0, ParameterDirection.Input, howMany);

            return dataMiner.GetListData(command);
        }

        public static DataTable GetNewsItemById(int id)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetNewsById";
            dataMiner.AddParameter(command, "@Id", DbType.Int32, 0, ParameterDirection.Input, id);
            return dataMiner.GetListData(command);
        }

        public static int UpdateNews(int id, string title, string shortDescription,
            string description, bool isActive, bool isFeatured)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateNews";
            dataMiner.AddParameter(command, "@Id", DbType.Int32, 0, ParameterDirection.Input, id);
            dataMiner.AddParameter(command, "@Title", DbType.String, -1, ParameterDirection.Input, title);
            dataMiner.AddParameter(command, "@ShortDescription", DbType.String, -1, ParameterDirection.Input, shortDescription);
            dataMiner.AddParameter(command, "@Description", DbType.String, -1, ParameterDirection.Input, description);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@IsFeatured", DbType.Boolean, 0, ParameterDirection.Input, isFeatured);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }
    }
}
